package com.fruit.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;

import net.coobird.thumbnailator.Thumbnails;


public class PicUtil {

	public static InputStream base64ToInputStream(String strBase64, boolean isCut){
		if (strBase64 == null)
			return null;
		
		
		try{
			byte[] b = Base64.decodeBase64(strBase64.split(",")[1]);			
			InputStream sbs = new ByteArrayInputStream(b); 
			if(isCut){
				InputStream sbsn = cutImage(sbs,200,200);
				return sbsn;
			}
			
			return sbs;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	//压缩图片
	public static InputStream cutImage(InputStream ins,int width, int height){
		if (ins == null)
			return null;
		
		ByteArrayOutputStream out = null;
		try{		
			out = new ByteArrayOutputStream();
			Thumbnails.of(ins).size(width, height).toOutputStream(out);
			InputStream sbsn = new ByteArrayInputStream(out.toByteArray()); 
	
			return sbsn;
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			if(ins != null){
				try {
					ins.close();
				} catch (IOException e) {}
			}
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {}
			}
			
		}
		
	}
	
	public static void main(String args[]){
		
        
	}

}
